package com.xbx.admin.controller.user;

import com.github.pagehelper.PageInfo;
import com.xbx.baseweb.constant.ResponseCode;
import com.xbx.baseweb.controller.BaseController;
import com.xbx.baseweb.controller.ResultUtils;
import com.xbx.baseweb.model.ResponseModel;
import com.xbx.common.constant.ResultConstant;
import com.xbx.common.utils.EntityUtils;
import com.xbx.admin.constant.UserManagerConstant;
import com.xbx.admin.entity.user.Module;
import com.xbx.admin.entity.user.Role;
import com.xbx.admin.entity.user.form.RoleInfoForm;
import com.xbx.admin.entity.user.form.SettingModuleForm;
import com.xbx.admin.entity.user.form.UserInfoForm;
import com.xbx.admin.entity.user.req.UserInfoReq;
import com.xbx.admin.entity.user.vo.ButtonAuthVo;
import com.xbx.admin.entity.user.vo.RoleVo;
import com.xbx.admin.entity.user.vo.UserVo;
import com.xbx.admin.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户管理
 *
 * @author 89005691
 * @create 2018-08-30 20:01
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/userController")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户信息列表", httpMethod = "POST", response = UserVo.class)
    @PostMapping(value = "/selectUserList")
    public ResponseModel selectUserList(@RequestBody UserInfoReq userInfoReq) {

        PageInfo pageInfo = this.userService.pageUserInfoList(userInfoReq);

        pageInfo.setList(EntityUtils.transform(pageInfo.getList(), UserVo.class));

        return ResultUtils.success("list", pageInfo);
    }

    @ApiOperation(value = "授予角色", httpMethod = "POST")
    @PostMapping(value = "/allocateRole")
    public ResponseModel allocateRole(@Valid @RequestBody UserInfoForm userInfoForm, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        try {

            return ResultUtils.success("obj", this.userService.allocateRole(userInfoForm));
        } catch (Exception e) {

            log.error("授予角色出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "删除用户", httpMethod = "POST")
    @PostMapping(value = "/deleteUser")
    public ResponseModel deleteUser(Long id) {

        if (id == null) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.MISS_INFO.getMessage());
        }

        try {

            String result = this.userService.deleteUser(id);

            if (ResultConstant.OPREATE_SUCCESS.equals(result)) {

                return ResultUtils.success(ResultConstant.OPREATE_SUCCESS);
            } else {

                return ResultUtils.fail(ResponseCode.BUSSINESS_FAIL.getCode(), result);
            }
        } catch (Exception e) {

            log.error("删除用户出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "查询角色信息列表", httpMethod = "POST", response = Role.class)
    @PostMapping(value = "/selectRoleList")
    public ResponseModel selectRoleList(@RequestBody UserInfoReq userInfoReq) {

        PageInfo pageInfo = this.userService.pageRoleInfoList(userInfoReq);

        pageInfo.setList(EntityUtils.transform(pageInfo.getList(), RoleVo.class));

        return ResultUtils.success("list", pageInfo);
    }

    @ApiOperation(value = "启用角色", httpMethod = "POST")
    @PostMapping(value = "/enabledRole")
    public ResponseModel enabledRole(Long id) {

        if (id == null) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.MISS_INFO.getMessage());
        }

        try {

            String result = this.userService.changeRole(id, UserManagerConstant.ROLE_STATUS_ENABLED);

            if (ResultConstant.OPREATE_SUCCESS.equals(result)) {

                return ResultUtils.success(ResultConstant.OPREATE_SUCCESS);
            }else {

                return ResultUtils.fail(ResponseCode.BUSSINESS_FAIL.getCode(), result);
            }
        } catch (Exception e) {

            log.error("启用角色出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "禁用角色", httpMethod = "POST")
    @PostMapping(value = "/disabledRole")
    public ResponseModel disabledRole(Long id) {

        if (id == null) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.MISS_INFO.getMessage());
        }

        try {

            String result = this.userService.changeRole(id, UserManagerConstant.ROLE_STATUS_DISABLED);

            if (ResultConstant.OPREATE_SUCCESS.equals(result)) {

                return ResultUtils.success(ResultConstant.OPREATE_SUCCESS);
            }else {

                return ResultUtils.fail(ResponseCode.BUSSINESS_FAIL.getCode(), result);
            }
        } catch (Exception e) {

            log.error("禁用角色出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "创建角色", httpMethod = "POST")
    @PostMapping(value = "/createRole")
    public ResponseModel createRole(@Valid @RequestBody RoleInfoForm roleInfoForm, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        try {

            String result = this.userService.createRole(roleInfoForm);

            if (ResultConstant.OPREATE_SUCCESS.equals(result)) {

                return ResultUtils.success(ResultConstant.OPREATE_SUCCESS);
            }else {

                return ResultUtils.fail(ResponseCode.BUSSINESS_FAIL.getCode(), result);
            }
        } catch (Exception e) {

            log.error("创建角色出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "查询菜单", httpMethod = "POST", response = Module.class)
    @PostMapping(value = "/selectModuleList")
    public ResponseModel selectModuleList() {

        // 不查询按钮
        List<Module> modules = this.userService.listModuleInfoList(false, null);

        return ResultUtils.success("list", modules);
    }

    @ApiOperation(value = "查询菜单用于设置权限", httpMethod = "POST", response = Module.class)
    @PostMapping(value = "/selectModuleAndButtonList")
    public ResponseModel selectModuleAndButtonList(Long roleId) {

        if (roleId == null) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.MISS_INFO.getMessage());
        }

        // 查询按钮
        List<Module> modules = this.userService.listModuleInfoList(true, roleId);

        return ResultUtils.success("list", modules);
    }

    @ApiOperation(value = "设置权限", httpMethod = "POST")
    @PostMapping(value = "/settingPermission")
    public ResponseModel settingPermission(@Valid @RequestBody SettingModuleForm settingModuleForm, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        try {

            return ResultUtils.success("obj", this.userService.settingPermission(settingModuleForm));
        } catch (Exception e) {

            log.error("设置权限出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "删除角色", httpMethod = "POST")
    @PostMapping(value = "/deleteRole")
    public ResponseModel deleteRole(Long id) {

        if (id == null) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.MISS_INFO.getMessage());
        }

        try {

            String result = this.userService.deleteRole(id);

            if (ResultConstant.OPREATE_SUCCESS.equals(result)) {

                return ResultUtils.success(ResultConstant.OPREATE_SUCCESS);
            } else {

                return ResultUtils.fail(ResponseCode.BUSSINESS_FAIL.getCode(), result);
            }
        } catch (Exception e) {

            log.error("删除角色出错", e);
            return ResultUtils.fail(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMessage());
        }
    }

    @ApiOperation(value = "根据code查询按钮是否有权限", response = ButtonAuthVo.class, httpMethod = "POST")
    @PostMapping(value = "/selectAuthByCodes")
    public ResponseModel selectAuthByCodes(String codes) {

        if (StringUtils.isEmpty(codes)) {

            return ResultUtils.fail(ResponseCode.INVALID_PARAM.getCode(), ResponseCode.MISS_INFO.getMessage());
        }

        return ResultUtils.success("list", EntityUtils.transform(userService.selectAuthByCodes(codes), ButtonAuthVo.class));
    }
}
